package com.huang.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.canal.entity.ESbean;
import com.huang.eduservice.entity.*;
import com.huang.eduservice.entity.frontVo.CourseFrontVo;
import com.huang.eduservice.entity.vo.CourseInfVo;
import com.huang.eduservice.entity.vo.CoursePublishVo;
import com.huang.eduservice.entity.vo.CourseWebVo;
import com.huang.eduservice.mapper.EduCourseMapper;
import com.huang.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.servicebase.execptionhandler.huangException;
import com.hunag.utils.orderVo.EduCourseOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;


/**
 * <p>
 * 璇剧▼ 鏈嶅姟瀹炵幇绫� * </p>
 *
 * @author testjava
 * @since 2021-03-21
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    EduCourseDescriptionService courseDescriptionService;
    @Autowired
    EduVideoService eduVideoService;
    @Autowired
    EduChapterService eduChapterService;
    @Autowired
    EduPublishService publishService;


    @Override
    @Transactional
    public String saveCourseVo(CourseInfVo courseInfVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfVo, eduCourse);
        int num = baseMapper.insert(eduCourse);
        if (num == 0) {
            throw new huangException(20001, "添加课程失败");
        }
        String id = eduCourse.getId();

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfVo.getDescription());
        eduCourseDescription.setId(id);

        courseDescriptionService.save(eduCourseDescription);

        EduPublish eduPublish = new EduPublish();
        eduPublish.setCourseId(id);
        eduPublish.setPublish(courseInfVo.getPublish());

        publishService.save(eduPublish);

        return id;
    }

    @Override
    public CourseInfVo getCourseInfo(String courseId) {
        EduCourseDescription description = courseDescriptionService.getById(courseId);
        EduCourse eduCourse = baseMapper.selectById(courseId);
        EduPublish publish = publishService.getPublish(courseId);
        CourseInfVo courseInfVo = new CourseInfVo();
        courseInfVo.setPublish(publish.getPublish());
        BeanUtils.copyProperties(description, courseInfVo);
        BeanUtils.copyProperties(eduCourse, courseInfVo);
        return courseInfVo;
    }

    @Override
    public void updateCourseInfo(CourseInfVo courseInfVo) {
        EduCourse eduCourse = new EduCourse();

        EduCourseDescription description = new EduCourseDescription();

        BeanUtils.copyProperties(courseInfVo, eduCourse);
        BeanUtils.copyProperties(courseInfVo, description);
        //修改  出版社
        EduPublish publish = new EduPublish();
        publish.setCourseId(courseInfVo.getId());
        publish.setPublish(courseInfVo.getPublish());

        eduCourse.setGmtCreate(new Date());
        eduCourse.setGmtModified(new Date());
        publishService.update(publish, new QueryWrapper<EduPublish>().eq("course_id", courseInfVo.getId()));
        baseMapper.updateById(eduCourse);

        description.setGmtCreate(new Date());
        description.setGmtModified(new Date());
        courseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo getPublishVo(String id) {
        CoursePublishVo publishVo = baseMapper.getPublishVo(id);
        return publishVo;
    }

    @Override
    public void removeCourse(String id) {


        eduVideoService.removeVideoByCourseId(id);


        eduChapterService.removeChapterByCourseId(id);

        courseDescriptionService.removeById(id);

        int i = baseMapper.deleteById(id);
        if (i == 0) {
            throw new huangException(20001, "s");
        }
    }

    @Override
    public List<EduCourse> getCouIndex() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").last("limit 8");
        List<EduCourse> list = baseMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        //2 根据讲师id查询所讲课程
        QueryWrapper wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, wrapper);

        List<EduCourse> records = pageParam.getRecords();

        long current = pageParam.getCurrent();

        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;

    }


    @Override
    public CourseWebVo getFrontCourseInfo(String courseId) {
        CourseWebVo courseWebVo = baseMapper.getFrontCourseInfo(courseId);
        return courseWebVo;
    }


    @Override
    public List<EduCourse> getindexAllCourse() {
        List<EduCourse> eduCourses = baseMapper.selectList(null);
        return eduCourses;
    }


    // 获取全部es需要插入的数据
    @Override
    public List<ESbean> getTotal() {
        return baseMapper.getTotal();
    }

    @Override
    public ESbean getEsBean(String id) {
      return   baseMapper.getEsbean(id);
    }

    @Override
    public List<ESbean> getEsbeanByteacherId(String id) {
        return baseMapper.getEsbeanByteacherId(id);
    }

    @Override
    public List<EduCourseOrder> getBuyCourseList(List<String> list) {
        List<EduCourseOrder> res=new ArrayList<>();

        for (String s : list) {
            QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
            wrapper.eq("id",s);
            EduCourse eduCourse = baseMapper.selectOne(wrapper);
            EduCourseOrder eduCourseOrder = new EduCourseOrder();
            BeanUtils.copyProperties(eduCourse,eduCourseOrder);
            res.add(eduCourseOrder);
        }
        return res;
    }


}
